/*
 *  This class is part of WorkTimeCalculator Android App
 *  Author: Domenico Pio Novelli
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.dpndeveloper.wtc.dialogs;

import java.util.Calendar;
import java.util.TimeZone;

import android.content.Context;
import android.widget.TextView;
import android.widget.TimePicker;

import com.dpndeveloper.wtc.R;
import com.dpndeveloper.wtc.R.id;
import com.dpndeveloper.wtc.dataprovider.WtcInterval;
import com.dpndeveloper.wtc.dataprovider.WtcIntervalDataBaseHelper;
import com.dpndeveloper.wtc.listener.OnIntervalSetListener;
import com.dpndeveloper.wtc.util.TimeUtil;

/**
 * Extension of Interval dialog to set the end of interval of current day
 * 
 * @author Domenico Pio Novelli
 * 
 */
public class EndAtDialog extends IntervalDialog {

	/**
	 * Constructor
	 * 
	 * @param context
	 *            The context where the View associated with this Dialog is
	 *            running
	 * @param initialInterval
	 *            the related interval
	 * @param onIntervalSetListener
	 *            an OnIntervalSetListener
	 * @param databaseHelper
	 *            Instance of DatabaseHelper to manage WtcIntervals
	 */
	public EndAtDialog(Context context, WtcInterval initialInterval,
			OnIntervalSetListener onIntervalSetListener,
			WtcIntervalDataBaseHelper databaseHelper) {

		super(context, initialInterval, onIntervalSetListener, databaseHelper,
				false);
		setTitle(R.string.workStopAt);
		updateStatAtLabel();
	}

	@Override
	public void setWtcInterval(WtcInterval initialInterval) {
		super.setWtcInterval(initialInterval);
		updateStatAtLabel();
	}

	/**
	 * Update the content of dialogs
	 */
	private void updateStatAtLabel() {
		TimePicker timepickerStart = (TimePicker) getView().findViewById(
				R.id.timePickerStartAt);
		timepickerStart.setEnabled(false);
		timepickerStart.getLayoutParams().height = 0;

		TextView startAtLabel = (TextView) getView().findViewById(
				id.textViewStartAtLabel);

		startAtLabel.setText(TimeUtil.toHoursMinutes(getInitialInterval()
				.getStartTime()));

	}

	@Override
	protected boolean isValid() {
		int endHourOfDay = getStopPicker().getCurrentHour();
		int endMinute = getStopPicker().getCurrentMinute();
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		boolean isValid = // If the set end time is in the future
		!(now.get(Calendar.HOUR_OF_DAY) < endHourOfDay || (now
				.get(Calendar.HOUR_OF_DAY) == endHourOfDay && now
				.get(Calendar.MINUTE) < endMinute));
		return super.isValid() && isValid;
	}
}
